<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
		<script type="text/javascript" src="../../js/boot.js"></script>
		<title>数据库视图</title>
	</head>

	<body>
		<div class="mini-splitter" style="width: 100%; height: 100%;">
			<div size="240" showCollapseButton="true">
				<div class="mini-fit">
					<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
						<a class="mini-button" iconCls="icon-add" onclick="openAddWin()">添加</a>
						<a class="mini-button" iconCls="icon-remove" onclick="doDelete()">删除</a>
						<!--<a class="mini-button" iconCls="icon-remove" plain="true" onclick="output()">删除</a>-->
						<a class="mini-menubutton " iconCls="icon-edit" menu="#popupMenu">操作</a>
						<ul id="popupMenu" class="mini-menu" style="display:none;">
							<li iconCls="icon-ok" onclick="doSearch()">执行查询</li>
							<li iconCls="icon-ok" onclick="doSave()">保存视图</li>
							<li iconCls="icon-ok" onclick="doSaveAll()">保存新版本</li>
							<li iconCls="icon-ok" onclick="doUpdateDatabase()">写入数据库</li>
							<li iconCls="icon-ok" onclick="">读取数据库</li>
							<li iconCls="icon-ok" onclick="">导入Excel</li>
							<li iconCls="icon-ok" onclick="">导出Excel</li>
						</ul>
					</div>
					<div id="tableList" class="mini-listbox" style="width:99%;height:94%;" textField="tableName" valueField="tableName">
					</div>
				</div>
			</div>

			<div showCollapseButton="true">
				<div id="tabs1" class="mini-tabs" activeIndex="0" style="width:100%;height:100%;" bodyStyle="padding:0;border:0;">
					<div title="视图信息">
						<div class="mini-layout" style="width:100%;height:100%;">
							<div region="north" height="200" showSplitIcon="true" showHeader="false">
								<textarea id="txaSql" class="mini-textarea" style="width:100%;height:100%;"></textarea>
							</div>
							<div title="center" region="center">
								<div class="mini-fit">
									<div id="datagrid" class="mini-datagrid" style="width:100%;height:100%;" showPager="true" allowResize="true" allowCellSelect="true" editNextOnEnterKey="true" editNextRowCell="true">
									</div>
								</div>
							</div>
						</div>
					</div>
					<div title="字段信息">
						<div class="mini-fit">
							<div id="fieldGrid" class="mini-datagrid" style="width:100%;height:100%;" showPager="false" allowResize="true" allowCellEdit="true" allowCellSelect="true" editNextOnEnterKey="true" editNextRowCell="true">
								<div property="columns">
									<div field="index" type="indexcolumn" width="30"></div>
									<div field="fieldID" visible="false"></div>
									<div name="fieldName" field="fieldName" headerAlign="center" width="150">字段名称
										<input property="editor" class="mini-textbox" style="width:100%;" minWidth="155" />
									</div>
									<div name="propertyName" field="propertyName" headerAlign="center" width="150">属性名称
										<input property="editor" class="mini-textbox" style="width:100%;" minWidth="155" />
									</div>
									<div name="description" field="description" headerAlign="center" width="150">字段说明
										<input property="editor" class="mini-textbox" style="width:100%;" minWidth="155" />
									</div>
									<div type="comboboxcolumn" autoShowPopup="true" name="type" field="type" width="150" align="center" headerAlign="center">数据类型
										<input property="editor" class="mini-combobox" style="width:100%;" data="CONST.get('dataTypes')" />
									</div>
									<div type="checkboxcolumn" field="pk" width="40" headerAlign="center">主键</div>
									<div type="checkboxcolumn" field="canNull" width="40" headerAlign="center">为空</div>
								</div>
							</div>
						</div>
					</div>
				</div>

			</div>
		</div>

		<div id="editTableWin" class="mini-window" title="" style="width:420px;height:300px;" showMaxButton="true" showCollapseButton="true" showShadow="true" showToolbar="true" showFooter="true" showModal="true" allowResize="true" allowDrag="true">
			<div id="editTableForm" style="padding:10px;">
				<table align="center">
					<tr>
						<td align="right">数据库名：</td>
						<td align="left"><input id="databaseName" name="databaseName" class="mini-combobox" textField="databaseName" valueField="databaseName" required="true" style="width: 300px;" /></td>
					</tr>
					<tr>
						<td align="right">表名称：</td>
						<td align="left"><input name="tableName" class="mini-textbox" required="true" style="width: 300px;"></td>
					</tr>
					<tr>
						<td align="right">表描述：</td>
						<td align="left"><input name="description" class="mini-textbox" required="true" style="width: 300px;"></td>
					</tr>
					<tr>
						<td align="right">类名：</td>
						<td align="left"><input name="className" class="mini-textbox" style="width: 300px;" /></td>
					</tr>
					<tr>
						<td align="right"></td>
						<td align="left">
							<a class="mini-button" iconCls="icon-save" onclick="doAddTable()">保存</a>
						</td>
					</tr>
				</table>
			</div>
		</div>

	</body>

	<script type="text/javascript">
		miniInit();

		var tableList = mini.get("tableList"); // 表定义列表
		tableList.set({
			url: http.getUrl("/admin/view/list.do")
		});

		var databaseName = mini.get("databaseName");
		databaseName.set({
			url: http.getUrl("/admin/database/list.do")
		});

		var txaSql = mini.get("txaSql");
		var datagrid = mini.get("datagrid");

		var editTableWin = mini.get("editTableWin"); // 编辑表定义窗口
		var editTableForm = new mini.Form("#editTableForm"); // 表定义表单
		var db1 = new mini.DataBinding();
		db1.bindForm("editTableForm", tableList); // 表定义列表与编辑表单数据绑定
		editTableWin.setTitle("修改视图信息");

		var fieldGrid = mini.get("fieldGrid"); // 字段定义列表

		var currentView = {}; // 当前编辑的视图
		tableList.on("itemclick",
			function(e) {
				var item = e.item;
				txaSql.setValue(item.viewSql);
				fieldGrid.setData(item.fields);
				datagrid.setData([]);
				currentView = item;
			});

		tableList.on("itemdblclick", function(e) {
			var item = e.item;
			editTableForm.setData(item);
			editTableWin.showAtPos('center', 'middle');
		});

		var isOpenAddWin = false;
		/*******************************************
		 * 打开新增窗体
		 *******************************************/
		function openAddWin() {
			isOpenAddWin = true;
			editTableForm.setData({});
			editTableWin.showAtPos('center', 'middle');
		}

		/*******************************************
		 * 保存新增表数据
		 *******************************************/
		function doAddTable() {
			tableList.addItem(editTableForm.getData());
			isOpenAddWin = false;
			editTableWin.hide();
			tableList.select(tableList.getData().length - 1);
		}

		/*******************************************
		 * 删除表
		 *******************************************/
		function doDelete() {
			if(tableList.getSelected()) {
				mini.confirm("确定要删除选中的视图吗？", "确定？",
					function(action) {
						if(action == "ok") {
							var table = tableList.getSelected();
							var data = {
								"databaseName": table.databaseName,
								"tableName": table.tableName
							};
							http.post("/admin/view/delete.do", data, function(page) {
								if(page.success) {
									mini.alert("数据库视图删除成功", "提示", null);
									tableList.removeItem(tableList.getSelected());
									fieldGrid.setData([]);
								} else {
									mini.alert(page.message, "操作失败", null);
								}
							});
						}
					});
			} else {
				mini.alert("请先选择要删除的表！", "错误", null);
			}
		}

		/*******************************************
		 * 保存一个视图至内存中
		 *******************************************/
		function doSave() {
			mini.confirm("确定要保存当前选中的视图吗？", "确定？",
				function(action) {
					if(action == "ok") {
						var bean = tableList.getSelected();
						var data = {};
						data.databaseName = bean.databaseName;
						data.tableName = bean.tableName;
						data.description = bean.description;
						data.className = bean.className;
						// data.fields = bean.fields;
						data.viewSql = txaSql.getValue();
						bean.viewSql = data.viewSql;
						bean.fields = fieldGrid.getData();
						if(bean.fields) {
							data.json = mini.encode(bean.fields);
						}
						debugger
						http.post("/admin/view/save.do", data, function(page) {
							if(page.success) {
								mini.alert("保存成功", "提示", null);
							} else {
								mini.alert(page.message, "保存失败", null);
							}
						});
					}
				}
			);
		}

		/*******************************************
		 * 保存所有视图数据至配置文件中
		 *******************************************/
		function doSaveAll() {
			// versionWin.hide();
			mini.confirm("确定要保存所有视图吗？", "确定？",
				function(action) {
					if(action == "ok") {
						var data = {};
						debugger
						http.post("/admin/view/saveAll.do", data, function(page) {
							if(page.success) {
								mini.alert("保存成功", "提示", null);
							} else {
								mini.alert(page.message, "保存失败", null);
							}
						});
					}
				}
			);
		}

		/*******************************************
		 * 将选中的视图更新至数据库中
		 *******************************************/
		function doUpdateDatabase() {
			if(tableList.getSelected()) {} else {
				mini.alert("请先选择要更新的视图！", "错误", null);
				return;
			}
			mini.confirm("确定要更新数据库表结构吗？", "确定？",
				function(action) {
					if(action == "ok") {
						var view = tableList.getSelected();
						var data = {
							"databaseName": view.databaseName,
							"tableName": view.tableName
						};
						debugger
						http.post("/admin/view/updateDatabase.do", data, function(page) {
							if(page.success) {
								mini.alert("数据库表结构更新成功", "提示", null);
							} else {
								mini.alert(page.message, "保存失败", null);
							}
						});
					}
				}
			);
		}

		/*******************************************
		 * 执行视图的SQL查询
		 *******************************************/
		function doSearch() {
			if(!tableList.getSelected()) {
				mini.alert("请先选择要执行的视图", "错误", null);
				return;
			}
			// 执行分页查询SQL语句
			var data = {
				"name": currentView.databaseName,
				"sql": currentView.viewSql
			};
			http.post("/admin/view/page.do", data, function(page) {
				if(page.success) {
					var columns = [{
						type: "indexcolumn"
					}];
					var titles;
					//
					debugger
					if(currentView.fields && currentView.fields.length > 0) {
						titles = currentView.fields;
					} else {
						titles = page.dataMap.fields;
						currentView.fields = titles;
					}
					for(var i = 0; i < titles.length; i++) {
						var column = {
							field: titles[i].propertyName,
							// width: 40,
							headerAlign: "center",
							header: titles[i].description
						};
						if(titles[i].type == "日期时间") {
							column.dateFormat = "yyyy-MM-dd HH:mm:ss";
						}
						columns.push(column);
					}
					datagrid.set({
						columns: columns
					});
					datagrid.setData(page.data);
					fieldGrid.setData(titles);
				} else {
					mini.alert(page.message, "查询失败", null);
				}
			});
		}
	</script>

</html>